package com.piggy.logi.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiWaybillEditExtBo;
import com.piggy.logi.sys.bo.LogiWaybillQueryBo;
import com.piggy.logi.sys.enums.DrayageStatusEnum;
import com.piggy.logi.sys.enums.ScheduleStatusEnum;
import com.piggy.logi.sys.enums.WaybillStatusEnum;
import com.piggy.logi.sys.service.ILogiWaybillService;
import com.piggy.logi.sys.vo.LogiWaybillExtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 运单信息Controller
 */
@Api(value = "运单信息控制器", tags = {"运单信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/waybill")
public class LogiWaybillController extends BaseController {

    private final ILogiWaybillService iLogiWaybillService;

    /**
     * 查询运单信息列表
     */
    @ApiOperation("查询运单信息列表")
    //@SaCheckPermission("sys:waybill:list")
    @PostMapping("/list")
    public TableDataInfo<LogiWaybillExtVo> list(@Validated @RequestBody LogiWaybillQueryBo bo) {
        if (ObjectUtil.isEmpty(bo.getDeptId())) {
            bo.setDeptId(SecurityUtils.getDeptId());
        }
        return iLogiWaybillService.queryPageList(bo);
    }

    /**
     * 导出运单信息列表
     */
    @ApiOperation("导出运单信息列表")
    //@SaCheckPermission("sys:waybill:export")
    @LogiLog(title = "运单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated @RequestBody LogiWaybillQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiWaybillExtVo> list = iLogiWaybillService.queryList(bo);
        ExcelUtil<LogiWaybillExtVo> util = new ExcelUtil<LogiWaybillExtVo>(LogiWaybillExtVo.class);
        util.exportExcel(response, list, "运单信息");
    }

    /**
     * 获取运单信息详细信息
     */
    @ApiOperation("获取运单信息详细信息")
    //@SaCheckPermission("sys:waybill:query")
    @GetMapping("/{id}")
    public R<LogiWaybillExtVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iLogiWaybillService.queryById(id));
    }

    /**
     * 新增运单信息
     */
    @ApiOperation("新增运单信息")
    //@SaCheckPermission("sys:waybill:add")
    @LogiLog(title = "运单信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<LogiWaybillExtVo> add(@Validated @RequestBody LogiWaybillEditExtBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiWaybillService.insertByAddBo(bo));
    }

    /**
     * 运单状态操作（提交、取消）
     */
    @ApiOperation("运单状态操作（提交、取消）")
    @LogiLog(title = "运单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/{type}/{waybillId}")
    public R<Void> operateWaybill(@ApiParam(value = "运单操作类型(100取消，110提交)") @PathVariable(value = "type") Integer type,
                                  @ApiParam(value = "运单Id") @NotNull(message = "运单id不能为空") @PathVariable(value = "waybillId") Long waybillId) {
        iLogiWaybillService.operateWaybill(type, waybillId);
        return R.ok();
    }

    /**
     * 修改运单信息
     */
    @ApiOperation("修改运单信息")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "运单信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiWaybillEditExtBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiWaybillService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除运单信息
     */
    @ApiOperation("删除运单信息")
    //@SaCheckPermission("sys:waybill:remove")
    @LogiLog(title = "运单信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long id) {
        return R.ok(iLogiWaybillService.deleteWithValidById(id, true) ? 1 : 0);
    }

    /**
     * 修改运单信息
     */
    @ApiOperation("运单状态修改")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "运单信息", businessType = BusinessType.UPDATE)
    @GetMapping("/editStatus/{billId}")
    public R<Integer> editStatus(@PathVariable("billId")Long billId, Integer status) {
        return R.ok(iLogiWaybillService.updateStatus(billId, status) ? 1 : 0);
    }

    /**
     * 单个运单调度状态修改
     */
    @ApiOperation("单个运单调度状态修改")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "单个运单调度状态修改", businessType = BusinessType.UPDATE)
    @GetMapping("/schedule/{billId}")
    public R<Integer> editScheduleStatus(@PathVariable("billId")Long billId, Integer scheduleStatus) {
        return R.ok(iLogiWaybillService.updateScheduleStatusByBillId(billId, scheduleStatus) ? 1 : 0);
    }

    /**
     * 单个运单取消调度
     */
    @ApiOperation("单个运单取消调度")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "单个运单取消调度", businessType = BusinessType.UPDATE)
    @GetMapping("/cancelScheduled/{billId}")
    public R<Integer> cancelScheduled(@PathVariable("billId")Long billId) {
        return R.ok(iLogiWaybillService.cancelScheduled(billId) ? 1 : 0);
    }

    /**
     * 单个运单取消调度
     */
    @ApiOperation("单个运单完成操作")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "单个运单完成操作", businessType = BusinessType.UPDATE)
    @GetMapping("/completed/{billId}")
    public R<Integer> completed(@PathVariable("billId")Long billId) {
        return R.ok(iLogiWaybillService.completed(billId) ? 1 : 0);
    }

    /**
     * 修改运单信息
     */
    @ApiOperation("运单短驳状态修改")
    //@SaCheckPermission("sys:waybill:edit")
    @LogiLog(title = "运单短驳状态修改", businessType = BusinessType.UPDATE)
    @GetMapping("/drayage/{billId}")
    public R<Integer> editDrayageStatus(@PathVariable("billId")Long billId, Integer drayageStatus) {
        return R.ok(iLogiWaybillService.updateDrayageStatusByBillId(billId, drayageStatus) ? 1 : 0);
    }

    /**
     * 查询可进行短驳运单信息列表
     */
    @ApiOperation("查询可发起或进行中短驳运单信息列表")
    //@SaCheckPermission("sys:waybill:list")
    @PostMapping("/drayageList")
    public TableDataInfo<LogiWaybillExtVo> drayageList(@Validated @RequestBody LogiWaybillQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setNotStatusList(Lists.newArrayList(0,
                WaybillStatusEnum.CANCEL.getCode(),
                WaybillStatusEnum.WAIT_SUBMIT.getCode(),
                WaybillStatusEnum.SIGNATURE.getCode(),
                WaybillStatusEnum.COMPLETED.getCode())
        );
        bo.setScheduleList(Lists.newArrayList(0,
                ScheduleStatusEnum.CANCEL_SCHEDULE.getCode())
        );
        bo.setDrayageList(Lists.newArrayList(0,
                DrayageStatusEnum.CANCEL.getCode()));
        return iLogiWaybillService.queryPageList(bo);
    }

    /**
     * 查询可进行短驳运单信息列表
     */
    @ApiOperation("查询进行中短驳运单信息列表")
    //@SaCheckPermission("sys:waybill:list")
    @PostMapping("/drayagingList")
    public TableDataInfo<LogiWaybillExtVo> drayagingList(@Validated @RequestBody LogiWaybillQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setDrayageList(Lists.newArrayList(DrayageStatusEnum.DEPARTURE.getCode(), DrayageStatusEnum.RECEIVED.getCode()));
        return iLogiWaybillService.queryPageList(bo);
    }

}
